<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="queryForm">
        <el-form-item>
          <el-input v-model="queryForm.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getDepartment">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.list" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="员工年龄" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="员工电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="员工邮箱" width="120" sortable>
      </el-table-column>
<!--      <el-table-column prop="salt" label="盐值" width="120" sortable>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="password" label="密码" width="120" sortable>-->
<!--      </el-table-column>-->
      <el-table-column prop="departmentId" label="所属部门ID" width="120" sortable>
      </el-table-column>
      <el-table-column prop="logininfoId" label="登录信息ID" width="120" sortable>
      </el-table-column>
      <el-table-column prop="shopId" label="店铺ID" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="员工状态" width="100" sortable>
        <template scope="scope">
          <span v-if="scope.row.state == 1" style="color: #18c79c">启用</span>
          <span v-else style="color: indianred">禁止</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchDelete" :disabled="this.sels.length===0">批量删除</el-button>
      <!--      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">-->
      <!--      </el-pagination>-->
      <el-pagination
          style="float:right;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryForm.currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="queryForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
    </el-col>

    <!--数据保存界面-->
    <el-dialog title="保存" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工名称" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="员工电话">
          <el-input v-model="saveForm.phone"auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工邮箱">
          <el-input v-model="saveForm.email"auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="saveForm.age"auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属部门">
          <el-input v-model="saveForm.departmentId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="登录信息">
          <el-input v-model="saveForm.logininfoId"auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺ID">
          <el-input v-model="saveForm.shopId"auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
// import util from '../../common/js/util'
// //import NProgress from 'nprogress'
// import { getUserListPage, removeUser, batchRemoveUser, editUser, saveUser } from '../../api/api';

export default {
  data() {
    return {
      queryForm: {
        keyword: "",
        pageSize: 5,
        currentPage: 1
      },
      pageInfo: {
        total: 0,
        list: []
      },
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//新增界面是否显示
      saveLoading: false,
      saveFormRules: {
        name: [
          {required: true, message: '请输入员工名称', trigger: 'blur'}
        ]
      },
      //新增界面数据
      saveForm: {
        id: null,
        username: "",
        state: null,
        phone:"",
        email:"",
        salt: "",
        password:"",
        age:null,
        departmentId:"",
        logininfoId:"",
        shopId:""
      }

    }
  },
  methods: {
    //修改显示
    handleEdit(index,row){
      this.saveFormVisible = true;
      this.saveForm = row;
    },
    //页码变化
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.getDepartment();
    },
    //当前页选择
    handleCurrentChange(val) {
      this.queryForm.currentPage = val;
      this.getDepartment();
    },
    //获取用户列表
    getDepartment() {
      this.listLoading = true;
      console.log(this.queryForm)
      this.$http.post("/emp", this.queryForm)
          .then(res => {
            var data = res.data;
            if (data.success) {
              this.pageInfo = data.data
              this.listLoading = false;
              console.log(res)
            }

          })
          .catch(res => {
            console.log("失败")
            console.log(this.pageInfo)
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/emp/" + row.id)
            .then(res => {
              var data = res.data
              if (data.success) {
                this.listLoading = false;
                // this.queryForm.currentPage = 1;
                // this.getDepartments()
                this.handleCurrentChange(1)
              }
            })
            .catch(res => {
            })
      }).catch(() => {
      });
    },

    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        name: "",
        state: null,
        managerId: null,
        parentId: null
      };
    },

    //保存
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            //NProgress.start();
            this.$http.put("/emp",this.saveForm)
            .then((res) => {
              this.saveLoading = false;
              var data = res.data;
              //NProgress.done();
              if (data.success) {
                this.saveLoading = false;
                this.saveFormVisible = false;
                this.handleCurrentChange(1);
              }
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchDelete: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/emp",ids)
            .then((res) => {
              var data = res.data;
              if(data.success){
                this.listLoading = false;
                this.handleCurrentChange(1);
              }
            });
      }).catch(() => {
        console.log("失败", res);
      });
    }
  },
  mounted() {
    this.getDepartment();
  }
}

</script>

<style scoped>

</style>